package com.cincc.cinccimdemo.ui.me.favorite;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.ui.me.favorite.viewholder.FavAudioContentViewHolder;
import com.cincc.cinccimdemo.ui.me.favorite.viewholder.FavContentViewHolder;
import com.cincc.cinccimdemo.ui.me.favorite.viewholder.FavFileContentViewHolder;
import com.cincc.cinccimdemo.ui.me.favorite.viewholder.FavImageContentViewHolder;
import com.cincc.cinccimdemo.ui.me.favorite.viewholder.FavTextContentViewHolder;
import com.cincc.cinccimdemo.ui.me.favorite.viewholder.FavUnknownContentViewHolder;
import com.cincc.cinccimdemo.ui.me.favorite.viewholder.FavVideoContentViewHolder;
import com.cincc.im.model.CollectModel;
import com.cincc.im.model.enums.MessageType;

import java.util.ArrayList;
import java.util.List;

public class FavoriteListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int CONVERSATION_TEXT = 0;
    private static final int CONVERSATION_IMAGE = 1;
    private static final int CONVERSATION_VOICE = 2;
    private static final int CONVERSATION_VIDEO = 3;
    private static final int CONVERSATION_FILE = 4;
    private static final int CONVERSATION_LOCATION = 5;
    private static final int CONVERSATION_CARD = 6;
    private static final int CONVERSATION_UNKNOWN = 7;
    private Activity mContext;
    private List<CollectModel> favoriteItems = new ArrayList<>();

    public FavoriteListAdapter(Activity context) {
        this.mContext = context;
    }

    public void setFavoriteList(List<CollectModel> list) {
        favoriteItems.clear();
        favoriteItems.addAll(list);
        notifyDataSetChanged();
    }

    public void addFavoriteItems(List<CollectModel> newItems) {
        this.favoriteItems.addAll(newItems);
    }

    public List<CollectModel> getFavoriteItems() {
        return favoriteItems;
    }

    public void removeFavoriteItem(String msgId) {
        int index = -1;
        for (int i = 0; i < this.favoriteItems.size(); i++) {
            if (favoriteItems.get(i).msgId.equals(msgId)) {
                index = i;
                break;
            }
        }
        if (index > -1) {
            this.favoriteItems.remove(index);
            notifyItemRangeRemoved(index, 1);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView;
        ViewStub viewStub;
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fav_content_container, parent, false);
        viewStub = itemView.findViewById(R.id.favContentViewStub);
        viewStub.setLayoutResource(getFavContentLayoutResource(viewType));
        viewStub.inflate();
        return getFavContentViewHolder(itemView, viewType);
    }

    @Override
    public int getItemViewType(int position) {
        CollectModel item = favoriteItems.get(position);
        if (item.messageType == MessageType.TEXT)
            return CONVERSATION_TEXT;
        else if (item.messageType == MessageType.IMAGE)
            return CONVERSATION_IMAGE;
        else if (item.messageType == MessageType.VIDEO)
            return CONVERSATION_VIDEO;
        else if (item.messageType == MessageType.VOICE)
            return CONVERSATION_VOICE;
        else if (item.messageType == MessageType.FILE)
            return CONVERSATION_FILE;
        else if (item.messageType == MessageType.CARD)
            return CONVERSATION_CARD;
        else if (item.messageType == MessageType.LOCATION)
            return CONVERSATION_LOCATION;
        else return CONVERSATION_UNKNOWN;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ((FavContentViewHolder) holder).bind(mContext, this.favoriteItems.get(position));
    }

    @Override
    public int getItemCount() {
        return this.favoriteItems == null ? 0 : this.favoriteItems.size();
    }

    private int getFavContentLayoutResource(int favType) {
        int layoutResourceId;
        switch (favType) {
            case CONVERSATION_TEXT:
                layoutResourceId = R.layout.fav_content_text;
                break;
            case CONVERSATION_IMAGE:
                layoutResourceId = R.layout.fav_content_image;
                break;
            case CONVERSATION_VIDEO:
                layoutResourceId = R.layout.fav_content_video;
                break;
            case CONVERSATION_FILE:
                layoutResourceId = R.layout.fav_content_file;
                break;
            case CONVERSATION_VOICE:
                layoutResourceId = R.layout.fav_content_audio;
                break;
            default:
                layoutResourceId = R.layout.fav_content_unkown;
                break;
        }
        return layoutResourceId;
    }

    private RecyclerView.ViewHolder getFavContentViewHolder(View itemView, int favType) {
        RecyclerView.ViewHolder viewHolder;
        switch (favType) {
            case CONVERSATION_TEXT:
                viewHolder = new FavTextContentViewHolder(itemView);
                break;
            case CONVERSATION_IMAGE:
                viewHolder = new FavImageContentViewHolder(itemView);
                break;
            case CONVERSATION_VIDEO:
                viewHolder = new FavVideoContentViewHolder(itemView);
                break;
            case CONVERSATION_FILE:
                viewHolder = new FavFileContentViewHolder(itemView);
                break;
            case CONVERSATION_VOICE:
                viewHolder = new FavAudioContentViewHolder(itemView);
                break;
            default:
                viewHolder = new FavUnknownContentViewHolder(itemView);
                break;
        }
        return viewHolder;
    }
}
